package com.jack.database_exp.controller;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jack.database_exp.bean.*;
import com.jack.database_exp.mapper.PlanInfoMapper;
import com.jack.database_exp.mapper.PlanMapper;
import com.jack.database_exp.mapper.WorkerMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Controller
@SuppressWarnings("all")
public class PlanController{
    @Autowired
    PlanMapper planMapper;
    @Autowired
    PlanInfoMapper planInfoMapper;
    @Autowired
    WorkerMapper workerMapper;
    @Autowired
    RedisTemplate<String,String> redisTemplate;
    @RequestMapping("/plan")
    public String plan(@RequestBody String json){
        System.out.println(json);
        String[] split = json.split(",\"t\",");
        json = "";
        for (int i=0;i<split.length-1;i++) {
            System.out.println(split[i]);
            json+=split[i];
            if(i!=split.length-2){
                json+=",";
            }
        }
        json+="]";
        String last = split[split.length-1].substring(0,split[split.length-1].length()-1);
        List<PlanMidBean> planMidBeans = JSONObject.parseArray(json, PlanMidBean.class);
        if(planMidBeans==null){
            return "/index";
        }
        for (PlanMidBean planMidBean : planMidBeans) {
            System.out.println(planMidBean);
        }
        Remark remark = JSON.parseObject(last, Remark.class);
        if(planMidBeans.size()==0){
            return "/index";
        }
        Integer WNo = workerMapper.queryWorkerByName(remark.getWorker());
        Plan plan = new Plan();
        Date date = new Date();
        System.out.println(remark);
        date.setYear(remark.getYear()-1900);
        date.setMonth(remark.getMonth()-1);
        System.out.println(date);
        plan.setPDate(date);
        plan.setWNo(WNo);
        planMapper.insertPaln(plan);
        Integer PNo = plan.getPNo();
        List<PlanInfo> planInfos = new ArrayList<>();
        for (PlanMidBean planMidBean : planMidBeans) {
            PlanInfo planInfo = new PlanInfo();
            planInfo.setGNo(planMidBean.getId());
            planInfo.setPINum(planMidBean.getNum());
            planInfo.setPNo(PNo);
            planInfo.setPrice(planMidBean.getPrice());
            planInfoMapper.insertInfo(planInfo);
            planInfos.add(planInfo);
        }
        String planInfos_JSON = JSONObject.toJSONString(planInfos);
        String plan_JSON = JSONObject.toJSONString(plan);
        redisTemplate.boundValueOps("planInfos_JSON").set(planInfos_JSON);
        redisTemplate.boundValueOps("plan_JSON").set(plan_JSON);
        return "index";
    }
    @RequestMapping("/showPlan")
    public ModelAndView showPlan(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("show");
        String planInfos_JSON = redisTemplate.boundValueOps("planInfos_JSON").get();
        String plan_JSON = redisTemplate.boundValueOps("plan_JSON").get();
        Plan plan = JSONObject.parseObject(plan_JSON,Plan.class);
        List<PlanInfo> planInfos = JSONObject.parseArray(planInfos_JSON,PlanInfo.class);
        modelAndView.addObject("plan",plan);
        modelAndView.addObject("planInfos",planInfos);
        modelAndView.addObject("name",workerMapper.queryWorkerById(plan.getWNo()));
        return modelAndView;
    }
}
